package ytr.android.sparrow;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * 麻雀スコア管理画面 パラメーター追加・更新ポップアップ。
 * @author toki
 *
 */
public class ScoreManagementPopup extends ScoreManagementActivity {

	/**
	 * ポップアップ生成用コンテキスト
	 */
	private Context m_Context;

	/**
	 * ポップアップ操作用オブジェクト
	 */
	private View mDialogView;
	private TextView m_ScoreText1;
	private TextView m_ScoreText2;
	private TextView m_ScoreText3;
	private TextView m_ScoreText4;
	private TextView m_ScoreText5;
	private TextView m_ScoreText6;
	private ImageButton m_SignButton1;
	private ImageButton m_SignButton2;
	private ImageButton m_SignButton3;
	private ImageButton m_SignButton4;
	private ImageButton m_SignButton5;
	private ImageButton m_SignButton6;

	/**
	 * スコア管理用リストビュー更新位置
	 */
	private int m_ListUpdatePosition;

	/**
	 * 描画用ハンドラクラスのオブジェクト
	 */
	private Handler m_DrawHandler;
	
	/**
	 * スコア入力エディット初期値
	 */
	private static final String DEFAULT_PARAMETER = "";
	
	/**
	 * スコア入力有り
	 */
	public static final String ACTIVE = "active";
	
	/**
	 * スコア入力無し
	 */
	public static final String PASSIVE = "passive";
	
	/**
	 * プラス符号を表すタグ
	 */
	public static final String PLUS = "+";
	
	/**
	 * マイナス符号を表すタグ
	 */
	public static final String MINUS = "-";

	/**
	 * コンストラクタ
	 * @param context
	 * @param handler
	 */
	public ScoreManagementPopup(Context context, Handler handler) {
		m_Context = context;
		m_DrawHandler = handler;
		setDialogView(context);
		setViewControl();
		setListener();
	}

	private void setDialogView(Context context) {
		LayoutInflater inflater = LayoutInflater.from(context);
		mDialogView = inflater.inflate(R.layout.add_score_dlalog, null);
	}

	private View getDialogView() {
		return mDialogView;
	}

	private void setListUpdatePosition(int position) {
		m_ListUpdatePosition = position;
	}

	private int getListUpdatePosition() {
		return m_ListUpdatePosition;
	}

	private void setViewControl() {
		m_ScoreText1 = (TextView) mDialogView.findViewById(R.id.addScorePopup_Text1);
		m_ScoreText2 = (TextView) mDialogView.findViewById(R.id.addScorePopup_Text2);
		m_ScoreText3 = (TextView) mDialogView.findViewById(R.id.addScorePopup_Text3);
		m_ScoreText4 = (TextView) mDialogView.findViewById(R.id.addScorePopup_Text4);
		m_ScoreText5 = (TextView) mDialogView.findViewById(R.id.addScorePopup_Text5);
		m_ScoreText6 = (TextView) mDialogView.findViewById(R.id.addScorePopup_Text6);
		
		m_SignButton1 = (ImageButton) mDialogView.findViewById(R.id.addScoreSign_1);
		m_SignButton2 = (ImageButton) mDialogView.findViewById(R.id.addScoreSign_2);
		m_SignButton3 = (ImageButton) mDialogView.findViewById(R.id.addScoreSign_3);
		m_SignButton4 = (ImageButton) mDialogView.findViewById(R.id.addScoreSign_4);
		m_SignButton5 = (ImageButton) mDialogView.findViewById(R.id.addScoreSign_5);
		m_SignButton6 = (ImageButton) mDialogView.findViewById(R.id.addScoreSign_6);
		
		m_SignButton1.setTag(PLUS);
		m_SignButton2.setTag(PLUS);
		m_SignButton3.setTag(PLUS);
		m_SignButton4.setTag(PLUS);
		m_SignButton5.setTag(PLUS);
		m_SignButton6.setTag(PLUS);
	}
	
	private void setListener() {
		SingButtonListener listener = new SingButtonListener();
		m_SignButton1.setOnClickListener(listener);
		m_SignButton2.setOnClickListener(listener);
		m_SignButton3.setOnClickListener(listener);
		m_SignButton4.setOnClickListener(listener);
		m_SignButton5.setOnClickListener(listener);
		m_SignButton6.setOnClickListener(listener);
	}

	private void setDefaultParameter(List<String> oldScore) {

		if (oldScore.size() == ScoreManagementActivity.LIST_VARIABLE_COLUMN_NUM) {
			m_ScoreText1.setText(oldScore.get(0));
			m_ScoreText2.setText(oldScore.get(1));
			m_ScoreText3.setText(oldScore.get(2));
			m_ScoreText4.setText(oldScore.get(3));
			m_ScoreText5.setText(oldScore.get(4));
			m_ScoreText6.setText(oldScore.get(5));
		} else {
			m_ScoreText1.setText(DEFAULT_PARAMETER);
			m_ScoreText2.setText(DEFAULT_PARAMETER);
			m_ScoreText3.setText(DEFAULT_PARAMETER);
			m_ScoreText4.setText(DEFAULT_PARAMETER);
			m_ScoreText5.setText(DEFAULT_PARAMETER);
			m_ScoreText6.setText(DEFAULT_PARAMETER);
		}
	}

	/**
	 *  麻雀スコア管理画面 点数追加・更新ポップアップ表示処理
	 */
	public void showScoreUpdateDialog(List<String> oldScore, int position) {

		setDefaultParameter(oldScore);    	
		setListUpdatePosition(position);

		new AlertDialog.Builder(m_Context)
		.setTitle(m_Context.getString(R.string.AddScore_Title))
		.setIcon(R.drawable.main_icon)
		.setView(getDialogView())
		.setPositiveButton(m_Context.getString(R.string.confirm), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				List<String> newScore = getScoreText();
				List<String> activeScore = getActiveScoreText();
				
				int activeScoreNum = checkActiveScoreNum(activeScore);
				if (LOWEST_USER_NUM <= activeScoreNum) {
	
					Bundle bundle = new Bundle();
					bundle.putStringArray(BUNDLE_KEY_SCORE_DATA, (String[])newScore.toArray(new String[0]));
					bundle.putStringArray(BUNDLE_KEY_ACTIVE_DATA, (String[])activeScore.toArray(new String[0]));
					bundle.putInt(BUNDLE_KEY_SCORE_POSITION, getListUpdatePosition());
					bundle.putInt(BUNDLE_KEY_DATA_TYPE, BUNDLE_KEY_DATA_TYPE_SCORE);
					
					Message msg = new Message();
					msg.setData(bundle);
					m_DrawHandler.sendMessage(msg);
				}
			}
		}).show();    	
	}

	/**
	 *  麻雀スコア管理画面 チップ・店料金追加・更新ポップアップ表示処理
	 */
	public void showResultUpdateDialog(List<String> oldScore, int position) {

		setDefaultParameter(oldScore);    	
		setListUpdatePosition(position);

		new AlertDialog.Builder(m_Context)
		.setTitle(m_Context.getString(R.string.AddSpecialPoint_Title))
		.setIcon(R.drawable.main_icon)
		.setView(getDialogView())
		.setPositiveButton(m_Context.getString(R.string.confirm), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				List<String> newScore = getScoreText();
				List<String> activeScore = getActiveScoreText();
				
				Bundle bundle = new Bundle();
				bundle.putStringArray(BUNDLE_KEY_SCORE_DATA, (String[])newScore.toArray(new String[0]));
				bundle.putStringArray(BUNDLE_KEY_ACTIVE_DATA, (String[])activeScore.toArray(new String[0]));
				bundle.putInt(BUNDLE_KEY_SCORE_POSITION, getListUpdatePosition());
				bundle.putInt(BUNDLE_KEY_DATA_TYPE, BUNDLE_KEY_DATA_TYPE_RESULT);
				
				Message msg = new Message();
				msg.setData(bundle);
				m_DrawHandler.sendMessage(msg);
			}
		}).show();    
	}

	private static String parseInputValue(String value, String sign) {
		String ret;
		try {
			if (sign.equals(PLUS)) {
				ret = String.format("%d", Integer.parseInt(value));
			} else {
				ret = String.format("-%d", Integer.parseInt(value));
			}
		} catch (NumberFormatException e) {
			ret = ScoreManagementBaseActivity.SCORE_LIST_DEFAULT_PARAMETER;
		}
		return ret;
	}
	
	private List<String> getScoreText() {
		List<String> ret = new ArrayList<String>();
		ret.add(ScoreManagementPopup.parseInputValue(m_ScoreText1.getText().toString(), m_SignButton1.getTag().toString()));
		ret.add(ScoreManagementPopup.parseInputValue(m_ScoreText2.getText().toString(), m_SignButton2.getTag().toString()));
		ret.add(ScoreManagementPopup.parseInputValue(m_ScoreText3.getText().toString(), m_SignButton3.getTag().toString()));
		ret.add(ScoreManagementPopup.parseInputValue(m_ScoreText4.getText().toString(), m_SignButton4.getTag().toString()));
		ret.add(ScoreManagementPopup.parseInputValue(m_ScoreText5.getText().toString(), m_SignButton5.getTag().toString()));
		ret.add(ScoreManagementPopup.parseInputValue(m_ScoreText6.getText().toString(), m_SignButton6.getTag().toString()));
		return ret;
	}
	
	private List<String> getActiveScoreText() {
		List<String> ret = new ArrayList<String>();
		boolean tmp;
		tmp = (m_ScoreText1.getText().toString().equals(DEFAULT_PARAMETER)) ? ret.add(PASSIVE) : ret.add(ACTIVE);
		tmp = (m_ScoreText2.getText().toString().equals(DEFAULT_PARAMETER)) ? ret.add(PASSIVE) : ret.add(ACTIVE);
		tmp = (m_ScoreText3.getText().toString().equals(DEFAULT_PARAMETER)) ? ret.add(PASSIVE) : ret.add(ACTIVE);
		tmp = (m_ScoreText4.getText().toString().equals(DEFAULT_PARAMETER)) ? ret.add(PASSIVE) : ret.add(ACTIVE);
		tmp = (m_ScoreText5.getText().toString().equals(DEFAULT_PARAMETER)) ? ret.add(PASSIVE) : ret.add(ACTIVE);
		tmp = (m_ScoreText6.getText().toString().equals(DEFAULT_PARAMETER)) ? ret.add(PASSIVE) : ret.add(ACTIVE);
		return ret;
	}
	
	private int checkActiveScoreNum(List<String> list) {
		int ret = 0;
		for (String val : list) {
			if (val.equals(ACTIVE)) {
				ret++;
			}
		}
		return ret;
	}
	
	public class SingButtonListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (PLUS == ((ImageButton)v).getTag()) {
				((ImageButton)v).setImageResource(R.drawable.minus);
				((ImageButton)v).setTag(MINUS);
			} else {
				((ImageButton)v).setImageResource(R.drawable.plus);
				((ImageButton)v).setTag(PLUS);
			}
		}
	}
}


